package com.scholar.goods.controller;

import cn.hutool.json.JSONUtil;
import com.scholar.shop.common.BusinessException;
import com.scholar.shop.common.CommonRes;
import com.scholar.shop.common.CommonUtil;
import com.scholar.shop.common.EmBusinessError;
import com.scholar.shop.seller.sku.api.SkuApi;
import com.scholar.shop.seller.sku.domain.GoodsSkuOpenVO;
import com.scholar.shop.seller.sku.domain.GoodsSkuRequest;
import com.scholar.shop.seller.stock.api.SkuStockApi;
import com.scholar.shop.seller.stock.domain.UpdateGoodsSkuStockRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.List;

/**
 * @author kimmy
 **/
@RestController
@RequestMapping("/seller/goods/sku")
public class GoodsSkuController {

    private static final org.slf4j.Logger LOG = org.slf4j.LoggerFactory.getLogger(GoodsSkuController.class);

    /**
     * 商品SKU操作的API
     */
    @Autowired
    private SkuApi skuApi;
    /**
     * 商品SKU库存操作的API
     */
    @Autowired
    private SkuStockApi skuStockApi;

    /**
     * 根据指定商品id查询其对应的商品SKU属性
     * @param goodsId 商品id
     * @return 结果
     */
    @GetMapping("/{goodsId}/list")
    public CommonRes<List<GoodsSkuOpenVO>> queryByGoodsId(@PathVariable("goodsId") String goodsId) throws Exception {

        if (StringUtils.isEmpty(goodsId)) {
            throw new BusinessException(EmBusinessError.SYS_REQUEST_PARAMETER_IS_NULL);
        }

        return skuApi.queryByGoodsId(goodsId);
    }

    /**
     * 更新商品的sku信息
     * @param request 商品SKU更新请求
     * @return 商品sku
     */
    @PutMapping
    public CommonRes<String> updateGoodsSku(@RequestBody GoodsSkuRequest request) throws Exception {

        if (StringUtils.isEmpty(request.getGoodsId())) {
            throw new BusinessException(EmBusinessError.SYS_REQUEST_PARAMETER_IS_NULL);
        }

        if (CollectionUtils.isEmpty(request.getSkuList())) {
            throw new BusinessException(EmBusinessError.SYS_REQUEST_PARAMETER_IS_NULL);
        }

        LOG.info("入参 [{}] ", JSONUtil.toJsonPrettyStr(request));

        return skuApi.updateGoodsSku(request);
    }

    /**
     * 查询多个商品的基本信息
     * @param skuIds sku主键id集合
     * @return 结果集
     */
    @GetMapping(value = "/{skuIds}/details")
    public CommonRes<List<GoodsSkuOpenVO>> getGoodsDetail(@PathVariable("skuIds") String skuIds) throws Exception {

        if (StringUtils.isEmpty(skuIds)) {
            throw new BusinessException(EmBusinessError.SYS_REQUEST_PARAMETER_IS_NULL);
        }

        return skuApi.getGoodsDetail(skuIds);
    }

    /**
     * 更新商品SKU库存
     * @param request 商品SKU库存的更新请求对象
     * @return 处理结果
     */
    @PutMapping("/stock")
    public CommonRes<String> updateGoodsStock(@RequestBody @Valid UpdateGoodsSkuStockRequest request,
                                              BindingResult result) throws BusinessException {

        if (result.hasErrors()) {
            throw new BusinessException(EmBusinessError.SYS_UNKNOWN_ERROR, CommonUtil.processErrorString(result));
        }

        return skuStockApi.updateGoodsSkuStock(request);
    }

}
